#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct LinkNode{
	char data;
	struct LinkNode *next;
}*LiStack;
static LiStack stack1 = NULL;
static LiStack stack2 = NULL;
char data;
void InitStack(LiStack *s){
//	printf("初始化中....\n");
	//生成一个头结点
	*s = (LiStack) malloc(sizeof(struct LinkNode));
	if(*s == NULL){
		printf("malloc fail\n");
		exit(0);
	}
//	printf("初始化成功！！\n");
	(*s)->next = NULL;
}
void output(){
	LiStack temp1 = stack1->next;
	LiStack temp2 = stack2->next;
	while (temp1) {
		printf("%c ",temp1->data);
		temp1 = temp1->next;
	}
	printf("\n+++++++++++\n");
	while (temp2) {
		printf("%c ",temp2->data);
		temp2 = temp2->next;
	}
	printf("\n");
}
bool StackEmpty(LiStack S){
	if(S->next == NULL){ //如果头节点下一个节点为空，就可以说明这个栈为空栈
		return true;
	}
	else {
		return false;
	}
}
void Push(){
	LiStack newNode = (LiStack) malloc(sizeof(struct LinkNode));
	newNode->data = data;
	//头插法
	newNode->next = stack1->next;
	stack1->next = newNode;
}

void changeData(){
	LiStack temp1 = stack1->next;
	while (temp1->next) {
		LiStack newNode = (LiStack) malloc(sizeof(struct LinkNode));
		newNode->data = temp1->data;
		//头插法
		newNode->next = stack2->next;
		stack2->next = newNode;
		LiStack temp2 = temp1;
		stack1->next = temp1->next;
		temp1 = temp1->next;
		free(temp2);
	}
//	LiStack temp2 = stack2->next;
//	while (temp2) {
//		printf("|%c ",temp2->data);
//		temp2 = temp2->next;
//	}
}
int Pop(){
	char temp;
	if(stack1->next && stack1->next->next){
		changeData();
		LiStack p = stack1->next;
		temp = stack1->next->data;
		free(p);
		stack1->next = NULL;
		
		LiStack temp = stack2->next;
		while (temp) {
			data = temp->data;
			Push();
			LiStack temp2 = temp;
			stack2->next = temp->next;
			temp = temp->next;
			free(temp2);
		}
	}
	else {
//		printf("121\n1212000");
		temp = stack1->next->data;
		stack1->next = NULL;
	}
	return temp -48;
}

int main(int argc, char *argv[]) {
	InitStack(&stack1);
	InitStack(&stack2);
	char buffer[20];
	while(scanf("%s",buffer)!=EOF){
		if(strlen(buffer) == 4){
			//入栈
			data = buffer[3];
//			printf("%c\n",data);
			Push();
//			output();
		}else {
			//出栈
			printf("%d\n",Pop());
//			Pop();
			//output();
		}
	
	}
}